﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;

namespace Surging.Core.Swagger
{
    public class SwaggerSerializerFactory
    {
        public static JsonSerializer Create(IOptions<JsonOptions> applicationJsonOptions)
        {
            JsonSerializerSettings _jsonSerializerSettings;
            if (applicationJsonOptions.Value.JsonSerializerOptions.PropertyNameCaseInsensitive)
            {
                _jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
                _jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                _jsonSerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
            }
            else
            {
                _jsonSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings();
                _jsonSerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                _jsonSerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
            }
            // TODO: Should this handle case where mvcJsonOptions.Value == null?
            return new JsonSerializer
            {
                NullValueHandling = NullValueHandling.Ignore,
                Formatting = Formatting.Indented,
                ContractResolver = new SwaggerContractResolver(_jsonSerializerSettings)
            };
        }
    }
}
